#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Author: Farmer Li, 公众号: 很酷的程序员/RealCoolEngineer
# @Date: 2023-01-27
# @Question: https://www.zhihu.com/question/580759829/answer/2862628452

import re


def method1(x: str):
    reg = re.compile(' +')
    return reg.sub(' ', x)


def method2(x: str):
    res = []
    for v in x:
        if res and res[-1] == ' ' and v == ' ':
            continue
        res.append(v)
    return ''.join(res)


def main():
    x = 'A   BB   CCC'
    print(f'By method 1: {method1(x)}')
    print(f'By method 2: {method2(x)}')
    a = list()


if __name__ == '__main__':
    main()
